﻿Public Class FileFilterControl
    'Private mIncludeIsDefault As Boolean
    'Private mExcludeIsDefault As Boolean
    Private mIsGlobal As Boolean

    Public Sub SetFilter(ByVal filter As Entities.Backup.FileFilter)
        SetFilter(filter, False)
    End Sub
    Public Sub SetFilter(ByVal filter As Entities.Backup.FileFilter, ByVal isGlobal As Boolean)
        mIsGlobal = isGlobal
        If filter IsNot Nothing Then
            With filter
                chkIncludeHiddenFiles.CheckState = BoolToCheckState(.IncludeHiddenFiles)
                chkIncludeSystemFiles.CheckState = BoolToCheckState(.IncludeSystemFiles)
                chkClearFileArchiveFlag.CheckState = BoolToCheckState(.ClearFileArchiveFlag)
                If filter.IncludePatterns IsNot Nothing Then
                    For Each clsPattern As Entities.MatchPattern In filter.IncludePatterns
                        UpdatePattern(lvwInclude, clsPattern, Nothing)
                    Next
                End If
                If filter.ExcludePatterns IsNot Nothing Then
                    For Each clsPattern As Entities.MatchPattern In filter.ExcludePatterns
                        UpdatePattern(lvwExclude, clsPattern, Nothing)
                    Next
                End If
            End With
        End If
        If Not mIsGlobal Then
            If lvwInclude.Items.Count = 0 Then ClearAndSetIncludeAsDefault()
            If lvwExclude.Items.Count = 0 Then ClearAndSetExcludeAsDefault()
        Else
            chkClearFileArchiveFlag.ThreeState = False
            chkIncludeHiddenFiles.ThreeState = False
            chkIncludeSystemFiles.ThreeState = False
        End If
    End Sub
    Private Sub UpdatePattern(ByVal listview As ListView, ByVal pattern As Entities.MatchPattern, ByVal currentItem As ListViewItem)
        If currentItem Is Nothing Then
            currentItem = listview.Items.Add(pattern.Type.ToString)
            currentItem.SubItems.Add(String.Empty)
        End If
        With currentItem
            .SubItems(1).Text = pattern.Pattern
            .Tag = pattern
        End With
    End Sub
    Private Sub ClearAndSetIncludeAsDefault()
        lvwInclude.Items.Clear()
        If Not mIsGlobal Then
            lvwInclude.Tag = True
            With lvwInclude.Items.Add("[Default]")
                .ForeColor = Drawing.SystemColors.InactiveCaptionText
                .Font = New Drawing.Font(Me.Font, Drawing.FontStyle.Italic)
            End With
        End If
    End Sub
    Private Sub ClearAndSetExcludeAsDefault()
        lvwExclude.Items.Clear()
        If Not mIsGlobal Then
            lvwExclude.Tag = True
            With lvwExclude.Items.Add("[Default]")
                .ForeColor = Drawing.SystemColors.InactiveCaptionText
                .Font = New Drawing.Font(Me.Font, Drawing.FontStyle.Italic)
            End With
        End If
    End Sub

    Private Function BoolToCheckState(ByVal value As Nullable(Of Boolean)) As CheckState
        If Not value.HasValue Then
            Return CheckState.Indeterminate
        ElseIf value.Value Then
            Return CheckState.Checked
        Else
            Return CheckState.Unchecked
        End If
    End Function
    Private Function CheckStateToBool(ByVal state As CheckState) As Nullable(Of Boolean)
        Select Case state
            Case CheckState.Checked
                Return True
            Case CheckState.Unchecked
                Return False
            Case Else
                Return Nothing
        End Select
    End Function

    Public Function GetFilter() As Entities.Backup.FileFilter
        Dim clsRet As New Entities.Backup.FileFilter
        If Not CType(lvwInclude.Tag, Boolean) Then
            If lvwInclude.Items.Count > 0 Then
                clsRet.IncludePatterns = New Generic.List(Of Entities.MatchPattern)
                For Each clsItem As ListViewItem In lvwInclude.Items
                    clsRet.IncludePatterns.Add(CType(clsItem.Tag, Entities.MatchPattern))
                Next
            End If
        End If
        If Not CType(lvwExclude.Tag, Boolean) Then
            If lvwExclude.Items.Count > 0 Then
                clsRet.ExcludePatterns = New Generic.List(Of Entities.MatchPattern)
                For Each clsItem As ListViewItem In lvwExclude.Items
                    clsRet.ExcludePatterns.Add(CType(clsItem.Tag, Entities.MatchPattern))
                Next
            End If
        End If
        clsRet.IncludeHiddenFiles = CheckStateToBool(chkIncludeHiddenFiles.CheckState)
        clsRet.IncludeSystemFiles = CheckStateToBool(chkIncludeSystemFiles.CheckState)
        clsRet.ClearFileArchiveFlag = CheckStateToBool(chkClearFileArchiveFlag.CheckState)
        Return clsRet
    End Function

    Private Sub btnInclude_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInclude_Clear.Click
        ClearAndSetIncludeAsDefault()
    End Sub

    Private Sub btnExclude_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExclude_Clear.Click
        ClearAndSetExcludeAsDefault()
    End Sub

    Private Sub btnExclude_Remove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExclude_Remove.Click
        If CType(lvwExclude.Tag, Boolean) Then Return
        While lvwExclude.SelectedItems.Count > 0
            lvwExclude.SelectedItems(0).Remove()
        End While
        If lvwExclude.Items.Count = 0 Then ClearAndSetExcludeAsDefault()
    End Sub

    Private Sub btnInclude_Remove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInclude_Remove.Click
        If CType(lvwInclude.Tag, Boolean) Then Return
        While lvwInclude.SelectedItems.Count > 0
            lvwInclude.SelectedItems(0).Remove()
        End While
        If lvwInclude.Items.Count = 0 Then ClearAndSetIncludeAsDefault()
    End Sub

    Private Sub btnInclude_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInclude_Add.Click
        EventHandler_EditPattern(lvwInclude, Nothing)
    End Sub
    Private Sub btnExclude_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExclude_Add.Click
        EventHandler_EditPattern(lvwExclude, Nothing)
    End Sub
    Private Sub EventHandler_EditPattern(ByVal listview As ListView, ByVal currentItem As ListViewItem)
        Try
            Dim clsPat As Entities.MatchPattern
            If currentItem Is Nothing Then
                clsPat = New Entities.MatchPattern
            Else
                clsPat = CType(currentItem.Tag, Entities.MatchPattern)
            End If
            Dim clsView As New FEditMatchPattern
            clsPat = clsView.ShowDialog(Me.FindForm, clsPat)
            If clsPat IsNot Nothing Then
                If CType(listview.Tag, Boolean) Then
                    listview.Items.Clear()
                    listview.Tag = False
                End If
                UpdatePattern(listview, clsPat, currentItem)
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub
    Private Sub EventHandler_EditSelectedPattern(ByVal listView As ListView)
        Try
            If listView.SelectedItems.Count <> 1 Then Beep() : Return
            If CBool(listView.Tag) Then Beep() : Return
            EventHandler_EditPattern(listView, listView.SelectedItems(0))
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub lvwInclude_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwInclude.DoubleClick
        EventHandler_EditSelectedPattern(lvwInclude)
    End Sub
    Private Sub lvwExclude_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwExclude.DoubleClick
        EventHandler_EditSelectedPattern(lvwExclude)
    End Sub
End Class
